//
//  QueryFlight.m
//  AirTicket
//
//  Created by imac.bmw on 10-12-21.
//  Copyright 2010 aircococ. All rights reserved.
//

#import "QueryFlight.h"


///
@implementation QueryFlight
@synthesize webData;
@synthesize orig;
@synthesize dest;
@synthesize flightDate;
@synthesize airway;
@synthesize theConnection;
@synthesize delegate;
@synthesize aTicket;
@synthesize result;
@synthesize currentString;
@synthesize ticketClass;

-(void)getTicketInfo {

	
	NSString *httpMessage = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
							 "<v:Envelope xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" xmlns:c=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:v=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
							 "<v:Header />\n"
							 "<v:Body>\n"
							 "<n0:queryFlightList id=\"o0\" c:root=\"1\" xmlns:n0=\"http://mall.tempus.com\">\n"
							 "<in0 i:type=\"d:string\">\n"
							 "&lt;?xml version='1.0' encoding='UTF-8' standalone='yes' ?&gt;&lt;request&gt;&lt;flagtype&gt;1&lt;/flagtype&gt;&lt;strorig&gt;%@&lt;/strorig&gt;&lt;strdest&gt;%@&lt;/strdest&gt;&lt;departuredate&gt;%@&lt;/departuredate&gt;&lt;optiontype&gt;1&lt;/optiontype&gt;&lt;clienttype&gt;TempusYFT&lt;/clienttype&gt;&lt;airline&gt;%@&lt;/airline&gt;&lt;/request&gt;\n"
							 "</in0>\n"
							 "</n0:queryFlightList>\n"
							 "</v:Body>\n"
							 "</v:Envelope>\n",orig,dest,flightDate,airway];
	
	NSLog(@"org = %@,dst=%@,date = %@,airway=%@",orig,dest,flightDate,airway);
	NSURL *url = [NSURL URLWithString:@"http://113.105.64.239/TicketMobile/services/Cococ"];
	NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
	NSString *msgLen = [[NSString alloc] initWithFormat:@"%d", [httpMessage length]];
	
	[theRequest addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
	[theRequest addValue:msgLen forHTTPHeaderField:@"Content-Length"];
	[theRequest setHTTPMethod:@"POST"]; 
	[theRequest setHTTPBody:[httpMessage dataUsingEncoding:NSUTF8StringEncoding]];
	

	self.theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
	
	if(theConnection)
	{
		self.webData = [[NSMutableData alloc] init];
		NSLog(@"Connected success!");
	}
	else {
		NSLog(@"Connected failed!");
	}
	
	[httpMessage release];
	[theRequest release];
	[msgLen release];
	
	
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	//连接成功把nsdata清零
	[self.webData setLength:0];
	NSLog(@"connection did receive response!");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[self.webData appendData:data];
	//NSLog(@"connection did receive data!");
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	NSLog(@"the connection with an Error:%@", error);
	[connection release];
	[self.webData release];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSString *theXML = [[[NSString alloc] initWithData:self.webData encoding:NSUTF8StringEncoding] autorelease];
	theXML = [theXML stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
	theXML = [theXML stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];

	NSArray *array1 = [[theXML componentsSeparatedByString:@"<ns1:out>"] retain];
	theXML = [array1 objectAtIndex:1];
	NSArray *array2 = [[theXML componentsSeparatedByString:@"</ns1:out>"] retain];
	theXML = [array2 objectAtIndex:0];
	NSMutableData *data = [[NSMutableData alloc] initWithData:[theXML dataUsingEncoding:NSUTF8StringEncoding]];

	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
	[parser setDelegate:self];
	[parser setShouldResolveExternalEntities:YES];
	[parser parse];
	[parser release];
	[data release];

	[array1 release];
	[array2 release];
		
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *) qualifiedName attributes:(NSDictionary *)attributeDict {

	if ([elementName isEqualToString:@"avresult"]) {
		result = [[QueryResults alloc]init];
	}
	else if ([elementName isEqualToString:@"retcode"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"msg"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"date"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"segment"]){
		aTicket = [[Tickets alloc]init];
	}
	else if ([elementName isEqualToString:@"fltno"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"airways"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"org"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"dst"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"deptime"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"arrtime"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"planesty"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"tax"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"fuel"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"meal"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"class"]){
		ticketClass = [[airClass alloc]init];
	}
	else if ([elementName isEqualToString:@"price"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"discount"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"cabin"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"count"]){
		currentString = [NSMutableString string];
	}
	else if ([elementName isEqualToString:@"classtype"]){
		currentString = [NSMutableString string];
	}
}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

	if ([elementName isEqualToString:@"avresult"]) {
		//[allMsg addObject:result];
		//[result release];
		[delegate didGetResluts:result];
		//NSMutableArray *array = result.ticketsArray;
//		Tickets *ticket = [[array objectAtIndex:0] retain];
//		NSMutableArray *array1 = ticket.classArray;
//		for (airClass *classtype in array1)
//		NSLog(@"%@",classtype.price);
//		[result release];
	}
	else if ([elementName isEqualToString:@"retcode"]) {
		result.retcode = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"msg"]){
		result.msg = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"date"]){
		result.date = currentString;
		currentString = nil;

	}
	else if ([elementName isEqualToString:@"segment"]){
		[result.ticketsArray addObject:aTicket];
		[aTicket release];
	}
	else if ([elementName isEqualToString:@"fltno"]){
		aTicket.fltno = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"airways"]){
		aTicket.airways = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"org"]){
		aTicket.org = currentString;
		currentString = nil;	
	}
	else if ([elementName isEqualToString:@"dst"]){
		aTicket.dst = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"deptime"]){
		aTicket.deptime = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"arrtime"]){
		aTicket.arrtime = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"planesty"]){
		aTicket.planesty = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"tax"]){
		aTicket.tax = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"fuel"]){
		aTicket.fuel = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"meal"]){
		aTicket.meal = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"class"]){
		[aTicket.classArray addObject:ticketClass];
		[ticketClass release];
	}
	else if ([elementName isEqualToString:@"price"]){
		ticketClass.price = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"discount"]){
		ticketClass.discount = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"cabin"]){
		ticketClass.cabin = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"count"]){
		ticketClass.ticketsCount = currentString;
		currentString = nil;
	}
	else if ([elementName isEqualToString:@"classtype"]){
		ticketClass.classtype = currentString;
		currentString = nil;
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	if (currentString) {
		[currentString appendString:string];
	}
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {

	NSLog(@"%@",[parseError localizedDescription]);
}

- (void)dealloc {
	//[ticketClass release];
	//[currentString release];
	[result release];
	//[aTicket release];
	//[theConnection release];
	//[ticket release];
//	[seat release];
	[orig release];
	[dest release];
	[flightDate release];
	[airway release];
	[webData release];
    [super dealloc];
}

@end
